//
//  TableHeaderView.m
//  lizhong
//
//  Created by 李忠 on 4/28/16.
//  Copyright © 2016 李忠. All rights reserved.
//

#import "SDCycleAdvertView.h"
#import "UIView+SDExtension.h"
#import "SVProgressHUD.h"
#import "UIImageView+WebCache.h"

#define kWidth (self.bounds.size.width)
#define kHeight (self.bounds.size.height)
#define kArrCount (self.imageArray.count)

@interface SDCycleAdvertView()<UIScrollViewDelegate>

@property (weak, nonatomic) UIScrollView *scrollView;
@property (weak, nonatomic) UIPageControl *pageControll;
@property (strong, nonatomic) NSTimer *timer;


@end

@implementation SDCycleAdvertView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        [self setupUI];
        [self setupLayout];
    }
    return self;
}

#pragma mark
#pragma mark - 添加控件
- (void)setupUI{
    //设置scrollView
    //启用scrollView的分页效果
    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectZero];
    ;
    self.scrollView = scroll;
    self.scrollView.frame = self.bounds;
    self.scrollView.pagingEnabled = YES;
    //关闭垂直滚动
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    //关闭弹簧效果
    self.scrollView.bounces = NO;
    //设置pageControll
    UIPageControl *page = [[UIPageControl alloc] initWithFrame:CGRectZero];
    page.sdX = self.center.x;
    self.pageControll = page;
    self.pageControll.currentPage = 0;
    self.pageControll.pageIndicatorTintColor = [UIColor yellowColor];
    self.pageControll.currentPageIndicatorTintColor = [UIColor redColor];
    self.scrollView.delegate = self;
    [self addSubview:self.scrollView];
    [self addSubview:self.pageControll];
}


- (void)setupLayout{
    
    //MARK: - 设置scrollview的约束
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];

    //MARK: - 设置pageController的约束
    [self.pageControll mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.trailing.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.height.mas_equalTo(20);
        
    }];
    
    
}

- (void)setImageArray:(NSArray *)imageArray{
    
    _imageArray = imageArray;
    self.pageControll.numberOfPages = imageArray.count;
    [self setupScrollSubviews];
    [self layoutScrollViewSubviews];
    [self setupTimer];
    
}

#pragma mark
#pragma mark - 添加scrollview子控件
- (void)setupScrollSubviews{
    
    UIImageView *image1 = [[UIImageView alloc] initWithFrame:CGRectZero];
    [image1 sd_setImageWithURL:[NSURL URLWithString:self.imageArray.lastObject]];
    [self.scrollView addSubview:image1];
    
    self.scrollView.contentOffset = CGPointMake(kWidth, 0);
    
    for (int i = 0; i < kArrCount; i++) {
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectZero];
        [image sd_setImageWithURL:[NSURL URLWithString:self.imageArray[i]]];
        [self.scrollView addSubview:image];
        
    }
    UIImageView *image2 = [[UIImageView alloc] initWithFrame:CGRectZero];
    [image2 sd_setImageWithURL:[NSURL URLWithString:self.imageArray.firstObject]];
    [self.scrollView addSubview:image2];
    
}
#pragma mark
#pragma mark - 当scrollview子控件添加完成，给scrollview子控件布局
- (void)layoutScrollViewSubviews{
    
    [self.scrollView.subviews mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:0 leadSpacing:0 tailSpacing:0   ];
    
    [self.scrollView.subviews mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.bottom.mas_equalTo(0);
        make.height.width.mas_equalTo(self.scrollView);
    }];
}



#pragma mark
#pragma mark - 实现滚动方法
- (void)setupTimer{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(nextpage) userInfo:nil repeats:YES];
}

- (void)nextpage{
    [self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x+kWidth, 0) animated:YES];
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    [self moveOffset];
}

#pragma mark
#pragma mark - 手动拖拽的时候停止定时器
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
    [self.timer invalidate];
    
}

#pragma mark
#pragma mark - 停止拖拽的时候停止定时器
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{

    [self setupTimer];
}

#pragma mark
#pragma mark - scrollview停止减速调用的方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [self moveOffset];
}

#pragma mark
#pragma mark - 判断偏移量是否超出数组中最后一张和第一张图片的位置
- (void)moveOffset{
    if (self.scrollView.contentOffset.x >= kWidth * (kArrCount + 1)) {
        self.scrollView.contentOffset = CGPointMake(kWidth, 0);
    }else if(self.scrollView.contentOffset.x <= 0){
        self.scrollView.contentOffset = CGPointMake(kWidth * kArrCount, 0);
    }
}

#pragma mark
#pragma mark - 当scrollview 在滚动的时候计算页码
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    NSInteger index = round(self.scrollView.contentOffset.x / kWidth -1);
    
    if (index == kArrCount) {
        index = 0;
    }else if(index == -1){
        index = kArrCount -1;
    }
    self.pageControll.currentPage = index;
    
}

@end
